﻿using Fonour.Domain.Entities;
using Fonour.Domain.IRepositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fonour.EntityFrameworkCore.Repositories
{
    public class RoleRepository : FonourRepositoryBase<Role>, IRoleRepository
    {
        public RoleRepository(FonourDbContext dbcontext) : base(dbcontext)
        {
        }

        public async Task<List<Guid>> GetAllMenuListByRoleAsync(Guid roleId)
        {
            var roleMenus = await _dbContext.Set<RoleMenu>().Where(it => it.RoleId == roleId).ToListAsync();
            return roleMenus.Select(p => p.MenuId).ToList();
        }

        public bool UpdateRoleMenu(Guid roleId, List<RoleMenu> roleMenus)
        {
            try
            {
                var oldDatas = _dbContext.Set<RoleMenu>().Where(it => it.RoleId == roleId).ToList();
                oldDatas.ForEach(it => _dbContext.Set<RoleMenu>().Remove(it));
                _dbContext.SaveChanges();
                _dbContext.Set<RoleMenu>().AddRange(roleMenus);
                _dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
